package com.aaa.servlet;

import java.util.Map;

import javax.servlet.annotation.WebServlet;

import com.aaa.annotation.RequestMapping;
import com.aaa.dao.ProductDAO;
import com.aaa.dao.impl.ProductDAOImpl;
import com.aaa.result.ResultEntity;
import com.aaa.util.DAOUtil;

@WebServlet("/product/*")
public class ProductServlet extends  BaseServlet{
	private static final long serialVersionUID = 1L;
	private ProductDAO proDAO = new ProductDAOImpl();
	
	
	@RequestMapping(url="search")
	public ResultEntity search(Map<String,Object> args) {
		//1 调用dao获取数据
		Map<String, Object> map = proDAO.queryProduct(args);
		//2 返回数据
		return  new  ResultEntity(10004, "查询成功", map);
	}
	
	
	
	/**
	 * 根据id获取某个商品
	 * 那怕我后崩了 也要正常给前台返回数据
	 * aop
	 */
	@RequestMapping(url = "get")
	public ResultEntity  get(Map<String,Object> args) {
		//1 调用dao 根据id获取某个商品的信息
		//这句代码会出现异常 如果前台传递过来的 id不能转换成整型就会报错  qweqwe
		try {
			Integer id =  Integer.parseInt(  ( String)args.get("id") ) ;
			Map<String, Object> pro = proDAO.getProductById(id);
			
			//2 返回数据
			if(pro != null) {
				return new ResultEntity(10002, "获取成功", pro);
			}
			return new ResultEntity(10003, "获取失败", null);
			
		}catch(Exception  e) {
			e.printStackTrace();
			// 记录日志到本地
			// 返回前台错误信息
			return new ResultEntity(20005, "对不起 程序员送外卖去了", null);
		}
	}

	
	/**
	 * 首页业务
	 */
	@RequestMapping(url = "list")
	public ResultEntity  list(Map<String,Object> args) {
		try {
			//1 调用dao获取分页数据
			Map<String, Object> map = proDAO.listProduct(args);
			//2 返回数据
			return new ResultEntity(10001, "获取成功", map);
		}catch(Exception e) {
			return new ResultEntity(20005, "对不起 程序员送外卖去了", null);
		}
	}

}
